// pages/person/person.js
const util=require('../../utils/util.js')
import { $wuxToptips } from '../../components/lib/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeName:getApp().globalData.presentTheme.deepColor.match(/wux-(\S*)/)[1],
    avatar:'/images/logo/logo.png',
    hasUinfo:false,
    nickName:'请先登录',
    actions: [
      // {
      //   type: 'default',
      //   text: '邮箱登录',
      // },
      {
        text: '微信登录',
        type: 'primary',
      }
    ],
    free_times:0,
    remain_times:0,
    inspectOptions:'',
    value2:['color','smooth','acnespotmole','wrinkle','eyesattr','blackheadpore','skinquality','skinface'],
    options2: [
      {
        title: '肤色',
        value: 'color',
        color: "royal"
      },
      {
        title: '光滑度',
        value: 'smooth',
        color: "royal"
      },
      {
        title: '痘、斑、痣',
        value: 'acnespotmole',
        color: "royal"
      },
      {
        title: '皱纹',
        value: 'wrinkle',
        color: "royal"
      },
      {
        title: '黑眼圈、眼袋',
        value: 'eyesattr',
        color: "royal"
      },
      {
        title: '毛孔、黑头',
        value: 'blackheadpore',
        color: "royal"
      }
      ,{
        title: '肤质',
        value: 'skinquality',
        color: "royal"
      }
      ,
      {
        title: '全选',
        value: 'skinface',
        color: "royal"
      }
    ],
    analyzeHistory:'',
    articleShare:'',
    articleEnshrine:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    let app=getApp();
    this.setData({
      pageHeight:app.globalData.windowHeight
    })
    if(app.globalData.hasUserInfo){
      // let id=(await util.getOpenID()).result.openid;
      // console.log(id);
      // let uInfo=(await util.queryDbByOpenId('UserPublicInfo',id))[0] ;
      // console.log(uInfo)
      // app.globalData.userInfo.avatarUrl=uInfo.avatarUrl;
      // app.globalData.userInfo.nickName=uInfo.nick_name;
      // app.globalData.userInfo.openId=id;
      this.setData({
        hasUinfo:app.globalData.hasUserInfo,
        avatar:app.globalData.userInfo.avatarUrl,
        nickName:app.globalData.userInfo.nickName,
        themeName:app.globalData.presentTheme.deepColor.match(/wux-(\S*)/)[1]
      })
      console.log(app.globalData.userInfo.nickName)
      let di=app.globalData.freeTimes-app.globalData.userInfo.amount;
      console.log("付费次数",app.globalData.userInfo.remainingTimes)
      console.log("免费次数",app.globalData.freeTimes)
      console.log("已测次数",app.globalData.userInfo.amount)
      var inspect=app.globalData.userInfo.skinAnalyzeInspect;
      inspect=inspect.split(",");//以逗号分割字符串
      console.log(inspect)
      // var remain=app.globalData.userInfo.remainingTimes+app.globalData.freeTimes-app.globalData.userInfo.amount
      this.setData({
        free_times:((app.globalData.userInfo.amount<=app.globalData.freeTimes)?di:0),
        remain_times:app.globalData.userInfo.remainingTimes,
        value2:inspect,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          current: 3,
          theme:getApp().globalData.presentTheme.midColor.match(/wux-(\S*)/)[1],
          themeName:getApp().globalData.presentTheme.deepColor.match(/wux-(\S*)/)[1],
        })
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

  ,
  onClickSkinOption:function(){
    let that=this;
    if(!getApp().globalData.hasUserInfo){
      wx.showToast({
        icon:'none',
        title: '仅会员可使用自定义测肤功能',
        duration:2000,
        success:res=>{
          that.setData({value2:[]})
        }
      })
    }
  }
  ,
  login:function(){
    let app=getApp();
    let that=this;
    if(!app.globalData.hasUserInfo){
      console.log('登录')
      wx.showToast({
        icon:'none',
        title: '欢迎使用魔镜',
        duration:2000
      })
      util.initAccount().then(()=>{
        if(app.globalData.userInfo.nickName||app.globalData.userInfo.avatarUrl) {
          that.setData({
            hasUinfo:true,
            avatar:app.globalData.userInfo.avatarUrl,
            nickName:app.globalData.userInfo.nickName,
            value2:(app.globalData.userInfo.skinAnalyzeInspect).split(',')
          })
          app.globalData.hasUserInfo=true;
        }
      })
    }else{
      wx.redirectTo({
        url: '/pages/person/person',
      })
    }
  }
  ,
  exit:function(){
    wx.exitMiniProgram({
      success:function(){
          wx.showToast({
            icon:'none',
            title: '退出程序',
            duration:1200
          })
      }
  })
  }
  ,onConfirm:function(res){
    console.log(res.detail.selectedValue)
    var inspectNorm = res.detail.selectedValue
    inspectNorm=inspectNorm.join();
    if(res.detail.selectedValue.length<=3){
      wx.showToast({
        icon:'error',
        title: '请至少选择3项',
        cancleColcr:'#000'
      })
    }
    else if((this.data.value2).join()!=inspectNorm){
      this.showToptips1();
      console.log(getApp().globalData.userInfo.openId)
      console.log(inspectNorm)
      util.updateDbByOpenId('UserPrivateInfo',getApp().globalData.userInfo.openId,{skinAnalyzeInspect:inspectNorm})//更新数据库
      getApp().globalData.userInfo.skinAnalyzeInspect=inspectNorm;//更新全局
      this.setData({value2:(res.detail.selectedValue)})//更新自己
    }
    else{
      wx.showToast({
        icon:'none',
        title: '未发生改动,保持原设置',
        duration:1500
      })
    }
  },
  showToptips1() {
    $wuxToptips().show({
      icon: 'success',
      hidden: false,
      text: '同步设置中',
      duration: 2500,
      success() {},
    })
  },
  openAboutMe(){

    wx.setClipboardData({
      
      data:"2018925336@qq.com",
      success (res) {
        wx.showToast({
          icon:'none',
          title: '已复制管理员邮箱',
          duration:2000
        })
      }
    })
  },
  myCollection(){
    wx.showToast({
      icon:'none',
      title: '该功能测试中',
    })
  }
})